﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.HtmlControls;

using Wiki.Web.Models;

namespace Wiki.Web.Views.Page
{
    public partial class HtmlRepresentation : ViewPage<WikiPage>
    {
        public HtmlLink AtomLink
        {
            get
            {
                HtmlLink value = new HtmlLink();
                value.Href = Request.Url.ToString(".html", ".atom");
                value.Attributes.Add("title", "You can track the wiki page by subscribing to this Atom feed.");
                value.Attributes.Add("rel", "alternate");
                value.Attributes.Add("type", "application/atom+xml");

                return value;
            }
        }

        public HtmlGenericControl Script
        {
            get
            {
                HtmlGenericControl value = new HtmlGenericControl();
                value.TagName = "script";
                value.Attributes.Add("type", "text/javascript");
                value.Attributes.Add("src", "/Content/Page.js");

                return value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.Header.Title = HttpUtility.HtmlEncode(this.ViewData.Title);

            Response.ContentType = "text/html";
            Response.AddHeader("Content-Language", "en");
            Response.AddHeader("Content-Location", Request.Url.ToString(".html", String.Empty));

            if (this.ViewData.IsAvailable)
            {
                this.Header.Controls.Add(this.AtomLink);

                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.Cache.SetLastModified(this.ViewData.LastModified.Value);
            }
            else
            {
                Response.StatusCode = 404;
            }

            this.Header.Controls.Add(this.Script);
        }
    }
}